using Domain.Entity;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
    public class CMKRepository : ICMKRepository
    {
        private readonly MyDbContext myDbContext;

        public CMKRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        //新增
        public async Task<bool> AddCMKAsync(CMK cmk)
        {
            myDbContext.CMK.Add(cmk);
            return await myDbContext.SaveChangesAsync() > 0;
        }
        //删除
        public async Task<bool> DeleteCMKAsync(int id)
        {
            CMK cmk = await myDbContext.CMK.FindAsync(id);
            if (cmk == null)
            {
                return false;
            }

            myDbContext.CMK.Remove(cmk);
            int changes = await myDbContext.SaveChangesAsync();
            return changes > 0;
        }

		public async Task<List<CMK>> GetCMKAsyncById(int id)
		{
            return await myDbContext.CMK.Where(e => e.MfkId == id).ToListAsync();
		}

		/// <summary>
		/// 查询
		/// </summary>
		/// <returns></returns>
		/// <exception cref="NotImplementedException"></exception>
		public async Task<List<CMK>> SelectCMKAsync()
        {
            return await myDbContext.CMK.ToListAsync();
        }
    }
}
